package com.itdct.onflow.system.dict.listener

import com.itdct.onflow.system.dict.constant.DictRedisKey
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.data.redis.connection.RedisConnectionFactory
import org.springframework.data.redis.listener.ChannelTopic
import org.springframework.data.redis.listener.RedisMessageListenerContainer
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter

/**
 * @author Zhouwx
 * @date 2025/9/5 10:28:11
 * @version 1.0
 * @description
 */
@ConditionalOnProperty(name = ["onflow.dict.enable"], havingValue = "true")
@Configuration
class SysDictRedisMessageConfig {
    @Bean
    fun redisMessageListenerContainer(
        connectionFactory: RedisConnectionFactory,
        listenerAdapter: MessageListenerAdapter
    ): RedisMessageListenerContainer {
        val container = RedisMessageListenerContainer()
        container.setConnectionFactory(connectionFactory)
        container.addMessageListener(listenerAdapter, ChannelTopic(DictRedisKey.DICT_REFRESH))
        return container
    }

    @Bean("sysDictMessageListenerAdapter")
    fun messageListenerAdapter(listener: SysDictRefreshListener): MessageListenerAdapter {
        return MessageListenerAdapter(listener)
    }
}